/*
 * @Author: maskMan
 * @LastEditTime: 2024-07-16 10:03:44
 * @Descripttion:音频管理
 */
import cacheStore from './cacheStore'
export default class AudioManager {
  /**
   * @description: 获取音乐配置
   * @param {string} key
   * @return {boolean}
   */
  static getAudioConf(key): any {
    let state = cacheStore.getPublicData(key)
    if (state === undefined) {
      state = true
    }
    return state
  }
  /**
   * @description: 播放音乐
   * @param {cc.AudioClip} clip
   * @param {boolean} loop 重复播放
   * @return {boolean}
   */
  static playMusic(clip: cc.AudioClip, loop: boolean = false): number {
    if (this.getAudioConf('music') == true) {
      if (cc.audioEngine.isMusicPlaying() == true) {
        cc.audioEngine.stopMusic()
      } else {
        cc.audioEngine.stopMusic()
        const id = cc.audioEngine.playMusic(clip, loop)
        cc.audioEngine.setVolume(id, 0.5)
        return id
      }
    }
    return null
  }
  /**
   * @description: 播放音效
   * @param {cc.AudioClip} clip
   * @param {boolean} loop
   * @return {boolean}
   */
  static playEffect(clip: cc.AudioClip, loop: boolean = false): any {
    if (this.getAudioConf('effect')) {
      return cc.audioEngine.playEffect(clip, loop)
    }
    return null
  }
  /**
   * @description: 打开音效开关
   * @return {void}
   */
  static openEffect() {
    cacheStore.setPublicData('effect', true)
  }
  /**
   * @description: 打开音乐开关
   * @return {void}
   */
  static openMusic() {
    cacheStore.setPublicData('music', true)
  }
  /**
   * @description: 打开所有音频开关
   * @return {void}
   */
  static openAll() {
    cacheStore.setPublicData('music', true)
    cacheStore.setPublicData('effect', true)
  }
  /**
   * @description: 关闭音效开关
   * @return {void}
   */
  static closeEffect() {
    cc.audioEngine.stopAllEffects()
    cacheStore.setPublicData('effect', false)
  }
  /**
   * @description: 关闭音乐开关
   * @return {void}
   */
  static closeMusic() {
    cc.audioEngine.pauseMusic()
    cacheStore.setPublicData('music', false)
  }
  /**
   * @description: 停止音乐
   * @return {void}
   */
  static stopMusic() {
    cc.audioEngine.stopMusic()
  }
  /**
   * @description: 关闭音频开关
   * @return {void}
   */
  static closeAll() {
    cc.audioEngine.stopAll()
    cacheStore.setPublicData('music', false)
    cacheStore.setPublicData('effect', false)
  }
}
